<?php
error_reporting(E_ALL & ~E_NOTICE & ~E_DEPRECATED);

class EcsStemming
{
    protected $pdo;
    
    public function __construct(\PDO $pdo = null)
    {
        if ($pdo == null) {
            $dsn      = 'mysql:dbname=qas_quran; host=localhost; port=3306';
            $username = 'root';
            $password = '1234';
            $options  = array(
                PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8',
            ); 

            try {
                $pdo  = new \PDO($dsn, $username, $password, $options);
                $pdo->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION);
                $this->pdo = $pdo;
            } catch (\PDOException $e) {
                ?>
                <div class="row">
                    <div class="col-lg-12">
                        <h1 class="page-header">ERROR</h1>
                    </div>
                </div>
                <?php
                echo '<div class="alert alert-danger">Connection failed : ' . $e->getMessage().'</div>';
                exit;
            }
        } else {
            $this->pdo = $pdo;
        }
    }
    /**
     * Stemming Kata
     * @param  string $kata kata seperti = mengubah
     * @return string = kata hasil stemming --> ubah
     */
    public function stemmingKata($kata)
    {
        $kataAsal = $kata;
        // 1. Cek Kata di Kamus jika Ada SELESAI 
        if ($this->cekKamus($kata)) {
            return $kata;
        }
        
        // 2. Buang Infection suffixes (\-lah", \-kah", \-ku", \-mu", atau \-nya") 
        $kata = $this->deleteInflectionSuffixes($kata);
            
        // 3. Buang Derivation suffix (\-i" or \-an") 
        $kata = $this->deleteDerivationSuffixes($kata);
            
        // 4. Buang Derivation prefix 
        $kata = $this->deleteDerivationPrefix($kata);
        
        return $kata;
    }
    
    /**
     * Stemming teks
     * @param  string $teks teks seperti = aku akan mengubah dunia
     * @return string = teks hasil stemming --> aku akan ubah dunia
     */
    public function stemmingTeks($teks)
    {
        $words    = explode(" ", $teks);
        $newWords = array();
        foreach ($words as $word) {
            $newWords[] = $this->stemmingKata($word);
        }
        
        $teks = implode(" ", $newWords);
        
        return $teks;
    }
    
    /**
     * Cari kata yang akan distem dalam kamus.
     * Jika ditemukan maka diasumsikan bahwa kata tersebut adalah root word.
     * @param  string $kata kata seperti = mengubah
     * @return boolean TRUE | FALSE
     */
    public function cekKamus($kata)
    {
        $sql = "SELECT * FROM kata_dasar WHERE kata = '".$kata."' LIMIT 1";
        foreach ($this->pdo->query($sql) as $row) {
            if ($kata == $row['kata']) {
                return true;
            }
        }
        return false;
    }
    
    /**
     * Hapus Inflection Suffixes ("-lah", "-kah", "-ku", "-mu", atau "-nya")
     * @param  string $kata kata seperti = apakah
     * @return string --> apa 
     */
    public function deleteInflectionSuffixes($kata)
    { 
        $kataAsal = $kata;
        // Cek Inflection Suffixes
        if (eregi('([km]u|nya|[kl]ah|pun)$', $kata)) {
        //~ if (preg_match('([km]u|nya|[kl]ah|pun)$', $kata)) {
            $__kata = eregi_replace('([km]u|nya|[kl]ah|pun)$', '', $kata);
            //~ $__kata = preg_replace('([km]u|nya|[kl]ah|pun)$', '', $kata);
            // Jika berupa particles ("-lah", "-kah", "-tah" atau "-pun")
            if (eregi('([klt]ah|pun)$', $kata)) {
            //~ if (preg_match('([klt]ah|pun)$', $kata)) {
                // Hapus Possesive Pronouns ("-ku", "-mu", atau "-nya")
                if (eregi('([km]u|nya)$', $__kata)) {
                //~ if (preg_match('([km]u|nya)$', $__kata)) {
                    $__kata__ = eregi_replace('([km]u|nya)$', '', $__kata);
                    //~ $__kata__ = preg_replace('([km]u|nya)$', '', $__kata);
                    return $__kata__;
                }
            }
            return $__kata;
        }
        return $kataAsal;
    }
    
    
    /**
     * Hapus Derivation Suffixes ("-i", "-an" atau "-kan")
     * @param  string $kata kata seperti = percayakan
     * @return string --> percaya 
     */
    public function deleteDerivationSuffixes($kata)
    {
        $kataAsal = $kata;
        // Cek Suffixes
        if (preg_match('/(kan)$/', $kata)) {
            $__kata = preg_replace('/(kan)$/', '', $kata);
            if ($this->cekKamus($__kata)) {
                return $__kata;
            }
        }
        // cek -kan
        if (preg_match('/(an|i)$/', $kata)) {
            $__kata__ = preg_replace('/(an|i)$/', '', $kata);
            if ($this->cekKamus($__kata__)) {
                return $__kata__;
            }
        }
        return $kataAsal;
    }
    
    /**
     * Hapus Derivation Prefix ("di-", "ke-", "se-", "te-", "be-", "me-", atau "pe-")
     * @param  string $kata kata seperti = sesudah
     * @return string --> sudah
     */
    public function deleteDerivationPrefix($kata)
    {
        $kataAsal = $kata;    
        /* ------ Tentukan Tipe Awalan ------------*/
        // Jika di-,ke-,se-
        if (preg_match('/^(di|[ks]e)\S{1,}/',$kata)) {
            $__kata = preg_replace('/^(di|[ks]e)/','',$kata);
            if ($this->cekKamus($__kata)) {
                return $__kata;
            }
            $__kata__ = $this->deleteDerivationSuffixes($__kata);
            if ($this->cekKamus($__kata__)) {
                return $__kata__;
            }
        }
        //Jika awalannya adalah "te-", "me-", "be-", atau "pe-"
        if (preg_match('/^([tmbp]e)\S{1,}/',$kata)) {
            /*------------ Awalan "be-", -----------------------------------*/
            // Jika awalan "be-"
            if (preg_match('/^(be)\S{1,}/',$kata)) {
                // aturan 1
                if (preg_match('/^(ber)[aiueo]\S{1,}/',$kata)) {
                    $__kata = preg_replace('/^(ber)/','',$kata);
                    if ($this->cekKamus($__kata)) {
                        return $__kata;
                    }
                    $__kata__ = $this->deleteDerivationSuffixes($__kata);
                    if ($this->cekKamus($__kata__)) {
                        return $__kata__;
                    }
                    $__kata = preg_replace('/^(ber)/','r',$kata);
                    if ($this->cekKamus($__kata)) {
                        return $__kata;
                    }
                    $__kata__ = $this->deleteDerivationSuffixes($__kata);
                    if ($this->cekKamus($__kata__)) {
                        return $__kata__;
                    }
                }
                //aturan  2
                if (preg_match('/^(ber)[^aiueor][[:alpha:]](?!er)\S{1,}/',$kata)) {
                    $__kata = preg_replace('/^(ber)/','',$kata);
                    if ($this->cekKamus($__kata)) {
                        return $__kata;
                    }
                    $__kata__ = $this->deleteDerivationSuffixes($__kata);
                    if ($this->cekKamus($__kata__)) {
                        return $__kata__;
                    }
                }
                //aturan  3
                if (preg_match('/^(ber)[^aiueor][[:alpha:]]er[aiueo]\S{1,}/',$kata)) {
                    $__kata = preg_replace('/^(ber)/','',$kata);
                    if ($this->cekKamus($__kata)) {
                        return $__kata;
                    }
                    $__kata__ = $this->deleteDerivationSuffixes($__kata);
                    if ($this->cekKamus($__kata__)) {
                        return $__kata__;
                    }
                }
                //aturan  4
                if (preg_match('/^belajar\S{0,}/',$kata)) {
                    $__kata = preg_replace('/^(bel)/','',$kata);
                    if ($this->cekKamus($__kata)) {
                        return $__kata;
                    }
                    $__kata__ = $this->deleteDerivationSuffixes($__kata);
                    if ($this->cekKamus($__kata__)) {
                        return $__kata__;
                    }
                }
                //aturan  5
                if (preg_match('/^(be)[^aiueolr]er[^aiueo]\S{1,}/',$kata)) {
                    $__kata = preg_replace('/^(be)/','',$kata);
                    if ($this->cekKamus($__kata)) {
                        return $__kata;
                    }
                    $__kata__ = $this->deleteDerivationSuffixes($__kata);
                    if ($this->cekKamus($__kata__)) {
                        return $__kata__;
                    }
                }
            }
            /*------------end "be-", ---------------------------------------------*/
            /*------------ Awalan "te-", ---------------------------------------------*/
            // Jika awalan "te-"
            if (preg_match('/^(te)\S{1,}/',$kata)) {
            
                if (preg_match('/^(terr)\S{1,}/',$kata)) { 
                    return $kata;
                }
                // aturan 6
                if (preg_match('/^(ter)[aiueo]\S{1,}/',$kata)) {
                    $__kata = preg_replace('/^(ter)/','',$kata);
                    if ($this->cekKamus($__kata)) {
                        return $__kata;
                    }
                    $__kata__ = $this->deleteDerivationSuffixes($__kata);
                    if ($this->cekKamus($__kata__)) {
                        return $__kata__;
                    }
                    $__kata = preg_replace('/^(ter)/','r',$kata);
                    if ($this->cekKamus($__kata)) {
                        return $__kata;
                    }
                    $__kata__ = $this->deleteDerivationSuffixes($__kata);
                    if ($this->cekKamus($__kata__)) {
                        return $__kata__;
                    }
                }
                // aturan 7
                if (preg_match('/^(ter)[^aiueor]er[aiueo]\S{1,}/',$kata)) {
                    $__kata = preg_replace('/^(ter)/','',$kata);
                    if ($this->cekKamus($__kata)) {
                        return $__kata;
                    }
                    $__kata__ = $this->deleteDerivationSuffixes($__kata);
                    if ($this->cekKamus($__kata__)) {
                        return $__kata__;
                    }
                }
                // aturan 8
                if (preg_match('/^(ter)[^aiueor](?!er)\S{1,}/',$kata)) {
                    $__kata = preg_replace('/^(ter)/','',$kata);
                    if ($this->cekKamus($__kata)) {
                        return $__kata;
                    }
                    $__kata__ = $this->deleteDerivationSuffixes($__kata);
                    if ($this->cekKamus($__kata__)) {
                        return $__kata__;
                    }
                }
                // aturan 9
                if (preg_match('/^(te)[^aiueor]er[aiueo]\S{1,}/',$kata)) {
                    $__kata = preg_replace('/^(te)/','',$kata);
                    if ($this->cekKamus($__kata)) {
                        return $__kata;
                    }
                    $__kata__ = $this->deleteDerivationSuffixes($__kata);
                    if ($this->cekKamus($__kata__)) {
                        return $__kata__;
                    }
                }
                // aturan 35
                if (preg_match('/^(ter)[^aiueor]er[^aiueo]\S{1,}/',$kata)) {
                    $__kata = preg_replace('/^(ter)/','',$kata);
                    if ($this->cekKamus($__kata)) {
                         return $__kata;
                    }

                    $__kata__ = $this->deleteDerivationSuffixes($__kata);
                    if ($this->cekKamus($__kata__)) {
                        return $__kata__;
                    }
                }
            }
            /*------------end "te-", ---------------------------------------------*/
            /*------------ Awalan "me-", ---------------------------------------------*/
            // Jika awalan "me-"
            if (preg_match('/^(me)\S{1,}/',$kata)) {
                // aturan 10
                if (preg_match('/^(me)[lrwyv][aiueo]/',$kata)) {
                    $__kata = preg_replace('/^(me)/','',$kata);
                    if ($this->cekKamus($__kata)) {
                        return $__kata;
                    }
                    $__kata__ = $this->deleteDerivationSuffixes($__kata);
                    if ($this->cekKamus($__kata__)) {
                        return $__kata__;
                    }
                }
                // aturan 11
                if (preg_match('/^(mem)[bfvp]\S{1,}/',$kata)) {
                    $__kata = preg_replace('/^(mem)/','',$kata);
                    if ($this->cekKamus($__kata)) {
                        return $__kata;
                    }
                    $__kata__ = $this->deleteDerivationSuffixes($__kata);
                    if ($this->cekKamus($__kata__)) {
                        return $__kata__;
                    }
                }
                // aturan 12
                if (preg_match('/^(mempe)\S{1,}/',$kata)) {
                    $__kata = preg_replace('/^(mem)/','pe',$kata);    
                    
                    if ($this->cekKamus($__kata)) {
                        
                        return $__kata;
                    }
                    $__kata__ = $this->deleteDerivationSuffixes($__kata);
                    if ($this->cekKamus($__kata__)) {
                        return $__kata__;
                    }
                }
                //aturan 13
                if (preg_match('/^(mem)((r[aiueo])|[aiueo])\S{1,}/', $kata)) {
                    $__kata = preg_replace('/^(mem)/','m',$kata);
                    if ($this->cekKamus($__kata)) {
                        return $__kata;
                    }
                    $__kata__ = $this->deleteDerivationSuffixes($__kata);
                    if ($this->cekKamus($__kata__)) {
                        return $__kata__;
                    }
                    $__kata = preg_replace('/^(mem)/','p',$kata);
                    if ($this->cekKamus($__kata)) {
                        return $__kata;
                    }
                    $__kata__ = $this->deleteDerivationSuffixes($__kata);
                    if ($this->cekKamus($__kata__)) {
                        return $__kata__;
                    }
                }
                // aturan 14
                if (preg_match('/^(men)[cdjszt]\S{1,}/',$kata)) {
                    $__kata = preg_replace('/^(men)/','',$kata);
                    if ($this->cekKamus($__kata)) {
                        return $__kata;
                    }
                    $__kata__ = $this->deleteDerivationSuffixes($__kata);
                    if ($this->cekKamus($__kata__)) {
                        return $__kata__;
                    }
                }
                //aturan 15
                if (preg_match('/^(men)[aiueo]\S{1,}/',$kata)) {
                    $__kata = preg_replace('/^(men)/','n',$kata);
                    if ($this->cekKamus($__kata)) {
                        return $__kata;
                    }
                    $__kata__ = $this->deleteDerivationSuffixes($__kata);
                    if ($this->cekKamus($__kata__)) {
                        return $__kata__;
                    }
                    $__kata = preg_replace('/^(men)/','t',$kata);
                    if ($this->cekKamus($__kata)) {
                        return $__kata;
                    }
                    $__kata__ = $this->deleteDerivationSuffixes($__kata);
                    if ($this->cekKamus($__kata__)) {
                        return $__kata__;
                    }
                }
                // aturan 16
                if (preg_match('/^(meng)[ghqk]\S{1,}/',$kata)) {
                    $__kata = preg_replace('/^(meng)/','',$kata);
                    if ($this->cekKamus($__kata)) {
                        return $__kata;
                    }
                    $__kata__ = $this->deleteDerivationSuffixes($__kata);
                    if ($this->cekKamus($__kata__)) {
                        return $__kata__;
                    }
                }
                // aturan 17
                if (preg_match('/^(meng)[aiueo]\S{1,}/',$kata)) {
                    $__kata = preg_replace('/^(meng)/','',$kata);
                    if ($this->cekKamus($__kata)) {
                        return $__kata;
                    }
                    $__kata__ = $this->deleteDerivationSuffixes($__kata);
                    if ($this->cekKamus($__kata__)) {
                        return $__kata__;
                    }
                    $__kata = preg_replace('/^(meng)/','k',$kata);
                    if ($this->cekKamus($__kata)) {
                        return $__kata;
                    }
                    $__kata__ = $this->deleteDerivationSuffixes($__kata);
                    if ($this->cekKamus($__kata__)) {
                        return $__kata__;
                    }
                    $__kata = preg_replace('/^(menge)/','',$kata);
                    if ($this->cekKamus($__kata)) {
                        return $__kata;
                    }
                    $__kata__ = $this->deleteDerivationSuffixes($__kata);
                    if ($this->cekKamus($__kata__)) {
                        return $__kata__;
                    }
                }
                // aturan 18
                if (preg_match('/^(meny)[aiueo]\S{1,}/',$kata)) {
                    $__kata = preg_replace('/^(meny)/','s',$kata);
                    if ($this->cekKamus($__kata)) {
                        return $__kata;
                    }
                    $__kata__ = $this->deleteDerivationSuffixes($__kata);
                    if ($this->cekKamus($__kata__)) {
                        return $__kata__;
                    }
                    $__kata = preg_replace('/^(me)/','',$kata);
                    if ($this->cekKamus($__kata)) {
                        return $__kata;
                    }
                    $__kata__ = $this->deleteDerivationSuffixes($__kata);
                    if ($this->cekKamus($__kata__)) {
                        return $__kata__;
                    }
                }
            }
            /*------------end "me-", ---------------------------------------------*/
            
            /*------------ Awalan "pe-", ---------------------------------------------*/
            // Jika awalan "pe-"
            if (preg_match('/^(pe)\S{1,}/',$kata)) {
                // aturan 20
                if (preg_match('/^(pe)[wy]\S{1,}/',$kata)) {
                    $__kata = preg_replace('/^(pe)/','',$kata);
                    if ($this->cekKamus($__kata)) {
                        return $__kata;
                    }
                    $__kata__ = $this->deleteDerivationSuffixes($__kata);
                    if ($this->cekKamus($__kata__)) {
                        return $__kata__;
                    }
                }
                // aturan 21
                if (preg_match('/^(per)[aiueo]\S{1,}/',$kata)) {
                    $__kata = preg_replace('/^(per)/','',$kata);
                    if ($this->cekKamus($__kata)) {
                        return $__kata;
                    }
                    $__kata__ = $this->deleteDerivationSuffixes($__kata);
                    if ($this->cekKamus($__kata__)) {
                        return $__kata__;
                    }
                    $__kata = preg_replace('/^(per)/','r',$kata);
                    if ($this->cekKamus($__kata)) {
                        return $__kata;
                    }
                    $__kata__ = $this->deleteDerivationSuffixes($__kata);
                    if ($this->cekKamus($__kata__)) {
                        return $__kata__;
                    }
                }
                // aturan 23
                if (preg_match('/^(per)[^aiueor][[:alpha:]](?!er)\S{1,}/',$kata)) {
                    $__kata = preg_replace('/^(per)/','',$kata);
                    if ($this->cekKamus($__kata)) {
                        return $__kata;
                    }

                    $__kata__ = $this->deleteDerivationSuffixes($__kata);
                    if ($this->cekKamus($__kata__)) {
                        return $__kata__;
                    }
                }
                // aturan 24
                if (preg_match('/^(per)[^aiueor][[:alpha:]](er)[aiueo]\S{1,}/',$kata)) {
                    $__kata = preg_replace('/^(per)/','',$kata);
                    if ($this->cekKamus($__kata)) {
                        return $__kata;
                    }

                    $__kata__ = $this->deleteDerivationSuffixes($__kata);
                    if ($this->cekKamus($__kata__)) {
                        return $__kata__;
                    }
                }
                // aturan  25
                if (preg_match('/^(pem)[bfv]\S{1,}/',$kata)) {
                    $__kata = preg_replace('/^(pem)/','',$kata);
                    if ($this->cekKamus($__kata)) {
                        return $__kata;
                    }

                    $__kata__ = $this->deleteDerivationSuffixes($__kata);
                    if ($this->cekKamus($__kata__)) {
                        return $__kata__;
                    }
                }
                // aturan 26
                if (preg_match('/^(pem)(r[aiueo]|[aiueo])\S{1,}/',$kata)) {
                    $__kata = preg_replace('/^(pem)/','m',$kata);
                    if ($this->cekKamus($__kata)) {
                        return $__kata;
                    }

                    $__kata__ = $this->deleteDerivationSuffixes($__kata);
                    if ($this->cekKamus($__kata__)) {
                        return $__kata__;
                    }
                    $__kata = preg_replace('/^(pem)/','p',$kata);
                    if ($this->cekKamus($__kata)) {
                        return $__kata;
                    }

                    $__kata__ = $this->deleteDerivationSuffixes($__kata);
                    if ($this->cekKamus($__kata__)) {
                        return $__kata__;
                    }
                }
                // aturan 27
                if (preg_match('/^(pen)[cdjzt]\S{1,}/',$kata)) {
                    $__kata = preg_replace('/^(pen)/','',$kata);
                    if ($this->cekKamus($__kata)) {
                        return $__kata;
                    }

                    $__kata__ = $this->deleteDerivationSuffixes($__kata);
                    if ($this->cekKamus($__kata__)) {
                        return $__kata__;
                    }
                }
                // aturan 28
                if (preg_match('/^(pen)[aiueo]\S{1,}/',$kata)) {
                    $__kata = preg_replace('/^(pen)/','n',$kata);
                    if ($this->cekKamus($__kata)) {
                        return $__kata;
                    }

                    $__kata__ = $this->deleteDerivationSuffixes($__kata);
                    if ($this->cekKamus($__kata__)) {
                        return $__kata__;
                    }
                    $__kata = preg_replace('/^(pen)/','t',$kata);
                    if ($this->cekKamus($__kata)) {
                        return $__kata;
                    }

                    $__kata__ = $this->deleteDerivationSuffixes($__kata);
                    if ($this->cekKamus($__kata__)) {
                        return $__kata__;
                    }
                }
                // aturan 29
                if (preg_match('/^(peng)[^aiueo]\S{1,}/',$kata)) {
                    $__kata = preg_replace('/^(peng)/','',$kata);
                    if ($this->cekKamus($__kata)) {
                        return $__kata;
                    }

                    $__kata__ = $this->deleteDerivationSuffixes($__kata);
                    if ($this->cekKamus($__kata__)) {
                        return $__kata__;
                    }
                }
                // aturan 30
                if (preg_match('/^(peng)[aiueo]\S{1,}/',$kata)) {
                    $__kata = preg_replace('/^(peng)/','',$kata);
                    if ($this->cekKamus($__kata)) {
                        return $__kata;
                    }

                    $__kata__ = $this->deleteDerivationSuffixes($__kata);
                    if ($this->cekKamus($__kata__)) {
                        return $__kata__;
                    }
                    $__kata = preg_replace('/^(peng)/','k',$kata);
                    if ($this->cekKamus($__kata)) {
                        return $__kata;
                    }

                    $__kata__ = $this->deleteDerivationSuffixes($__kata);
                    if ($this->cekKamus($__kata__)) {
                        return $__kata__;
                    }
                    $__kata = preg_replace('/^(penge)/','',$kata);
                    if ($this->cekKamus($__kata)) {
                        return $__kata;
                    }

                    $__kata__ = $this->deleteDerivationSuffixes($__kata);
                    if ($this->cekKamus($__kata__)) {
                        return $__kata__;
                    }
                }
                // aturan 31
                if (preg_match('/^(peny)[aiueo]\S{1,}/',$kata)) {
                    $__kata = preg_replace('/^(peny)/','s',$kata);
                    if ($this->cekKamus($__kata)) {
                        return $__kata;
                    }

                    $__kata__ = $this->deleteDerivationSuffixes($__kata);
                    if ($this->cekKamus($__kata__)) {
                        return $__kata__;
                    }
                    $__kata = preg_replace('/^(pe)/','',$kata);
                    if ($this->cekKamus($__kata)) {
                        return $__kata;
                    }

                    $__kata__ = $this->deleteDerivationSuffixes($__kata);
                    if ($this->cekKamus($__kata__)) {
                        return $__kata__;
                    }
                }
                // aturan 32
                if (preg_match('/^(pel)[aiueo]\S{1,}/',$kata)) {
                    $__kata = preg_replace('/^(pel)/','l',$kata);
                    if ($this->cekKamus($__kata)) {
                        return $__kata;
                    }

                    $__kata__ = $this->deleteDerivationSuffixes($__kata);
                    if ($this->cekKamus($__kata__)) {
                        return $__kata__;
                    }
                }
                
                if (preg_match('/^(pelajar)\S{0,}/',$kata)) {
                    $__kata = preg_replace('/^(pel)/','',$kata);
                    if ($this->cekKamus($__kata)) {
                        return $__kata;
                    }

                    $__kata__ = $this->deleteDerivationSuffixes($__kata);
                    if ($this->cekKamus($__kata__)) {
                        return $__kata__;
                    }
                }
                // aturan 33
                if (preg_match('/^(pe)[^rwylmn]er[aiueo]\S{1,}/',$kata)) {
                    $__kata = preg_replace('/^(pe)/','',$kata);
                    if ($this->cekKamus($__kata)) {
                        return $__kata;
                    }

                    $__kata__ = $this->deleteDerivationSuffixes($__kata);
                    if ($this->cekKamus($__kata__)) {
                        return $__kata__;
                    }
                }
                // aturan 34
                if (preg_match('/^(pe)[^rwylmn](?!er)\S{1,}/',$kata)) {
                    $__kata = preg_replace('/^(pe)/','',$kata);
                    if ($this->cekKamus($__kata)) {
                        return $__kata;
                    }

                    $__kata__ = $this->deleteDerivationSuffixes($__kata);
                    if ($this->cekKamus($__kata__)) {
                        return $__kata__;
                    }
                }
                // aturan 36
                if (preg_match('/^(pe)[^aiueor]er[^aiueo]\S{1,}/',$kata)) {
                    $__kata = preg_replace('/^(pe)/','',$kata);
                    if ($this->cekKamus($__kata)) {
                        return $__kata;
                    }

                    $__kata__ = $this->deleteDerivationSuffixes($__kata);
                    if ($this->cekKamus($__kata__)) {
                        return $__kata__;
                    }
                }
            }
        }
            /*------------end "pe-", ---------------------------------------------*/
            /*------------ Awalan "memper-", ---------------------------------------------*/
        if (preg_match('/^(memper)\S{1,}/',$kata)) {
            $__kata = preg_replace('/^(memper)/','',$kata);
            if ($this->cekKamus($__kata)) {
                return $__kata;
            }
            $__kata__ = $this->deleteDerivationSuffixes($__kata);
            if ($this->cekKamus($__kata__)) {
                return $__kata__;
            }
            //*-- Cek luluh -r ----------
            $__kata = preg_replace('/^(memper)/','r',$kata);
            if ($this->cekKamus($__kata)) {
                return $__kata;
            }
            $__kata__ = $this->deleteDerivationSuffixes($__kata);
            if ($this->cekKamus($__kata__)) {
                return $__kata__;
            }
        }
        if (preg_match('/^(mempel)\S{1,}/',$kata)) {
            $__kata = preg_replace('/^(mempel)/','',$kata);
            if ($this->cekKamus($__kata)) {
                return $__kata;
            }
            $__kata__ = $this->deleteDerivationSuffixes($__kata);
            if ($this->cekKamus($__kata__)) {
                return $__kata__;
            }
            //*-- Cek luluh -r ----------
            $__kata = preg_replace('/^(mempel)/','l',$kata);
            if ($this->cekKamus($__kata)) {
                return $__kata;
            }
            $__kata__ = $this->deleteDerivationSuffixes($__kata);
            if ($this->cekKamus($__kata__)) {
                return $__kata__;
            }
        }
        if (preg_match('/^(menter)\S{1,}/',$kata)) {
            $__kata = preg_replace('/^(menter)/','',$kata);
            if ($this->cekKamus($__kata)) {
                return $__kata;
            }
            $__kata__ = $this->deleteDerivationSuffixes($__kata);
            if ($this->cekKamus($__kata__)) {
                return $__kata__;
            }
            //*-- Cek luluh -r ----------
            $__kata = preg_replace('/^(menter)/','r',$kata);
            if ($this->cekKamus($__kata)) {
                return $__kata;
            }
            $__kata__ = $this->deleteDerivationSuffixes($__kata);
            if ($this->cekKamus($__kata__)) {
                return $__kata__;
            }
        }
        if (preg_match('/^(member)\S{1,}/',$kata)) {
            $__kata = preg_replace('/^(member)/','',$kata);
            if ($this->cekKamus($__kata)) {
                return $__kata;
            }
            $__kata__ = $this->deleteDerivationSuffixes($__kata);
            if ($this->cekKamus($__kata__)) {
                return $__kata__;
            }
            //*-- Cek luluh -r ----------
            $__kata = preg_replace('/^(member)/','r',$kata);
            if ($this->cekKamus($__kata)) {
                return $__kata;
            }
            $__kata__ = $this->deleteDerivationSuffixes($__kata);
            if ($this->cekKamus($__kata__)) {
                return $__kata__;
            }
        }
    /*------------end "diper-", ---------------------------------------------*/
        if (preg_match('/^(diper)\S{1,}/',$kata)) {
            $__kata = preg_replace('/^(diper)/','',$kata);
            if ($this->cekKamus($__kata)) {
                return $__kata;
            }
            $__kata__ = $this->deleteDerivationSuffixes($__kata);
            if ($this->cekKamus($__kata__)) {
                return $__kata__;
            }
            /*-- Cek luluh -r ----------*/
            $__kata = preg_replace('/^(diper)/','r',$kata);
            if ($this->cekKamus($__kata)) {
                return $__kata;
            }
            $__kata__ = $this->deleteDerivationSuffixes($__kata);
            if ($this->cekKamus($__kata__)) {
                return $__kata__;
            }
        }
        /*------------end "diper-", ---------------------------------------------*/
        /*------------end "diter-", ---------------------------------------------*/
        if (preg_match('/^(diter)\S{1,}/',$kata)) {
            $__kata = preg_replace('/^(diter)/','',$kata);
            if ($this->cekKamus($__kata)) {
                return $__kata;
            }
            $__kata__ = $this->deleteDerivationSuffixes($__kata);
            if ($this->cekKamus($__kata__)) {
                return $__kata__;
            }
            /*-- Cek luluh -r ----------*/
            $__kata = preg_replace('/^(diter)','r',$kata);
            if ($this->cekKamus($__kata)) {
                return $__kata;
            }
            $__kata__ = $this->deleteDerivationSuffixes($__kata);
            if ($this->cekKamus($__kata__)) {
                return $__kata__;
            }
        }
        /*------------end "diter-", ---------------------------------------------*/
        /*------------end "dipel-", ---------------------------------------------*/
        if (preg_match('/^(dipel)\S{1,}/',$kata)) {
            $__kata = preg_replace('/^(dipel)/','l',$kata);
            if ($this->cekKamus($__kata)) {
                return $__kata;
            }
            $__kata__ = $this->deleteDerivationSuffixes($__kata);
            if ($this->cekKamus($__kata__)) {
                return $__kata__;
            }
            /*-- Cek luluh -l----------*/
            $__kata = preg_replace('/^(dipel)/','',$kata);
            if ($this->cekKamus($__kata)) {
                return $__kata;
            }
            $__kata__ = $this->deleteDerivationSuffixes($__kata);
            if ($this->cekKamus($__kata__)) {
                return $__kata__;
            }
        }
        /*------------end "dipel-", ---------------------------------------------*/
        if (preg_match('/^(diber)\S{1,}/',$kata)) {
            $__kata = preg_replace('/^(diber)/','',$kata);
            if ($this->cekKamus($__kata)) {
                return $__kata;
            }
            $__kata__ = $this->deleteDerivationSuffixes($__kata);
            if ($this->cekKamus($__kata__)) {
                return $__kata__;
            }
            /*-- Cek luluh -l----------*/
            $__kata = preg_replace('/^(diber)/','r',$kata);
            if ($this->cekKamus($__kata)) {
                return $__kata;
            }
            $__kata__ = $this->deleteDerivationSuffixes($__kata);
            if ($this->cekKamus($__kata__)) {
                return $__kata__;
            }
        }
        if (preg_match('/^(keber)\S{1,}/',$kata)) {
            $__kata = preg_replace('/^(keber)/','',$kata);
            if ($this->cekKamus($__kata)) {
                return $__kata;
            }
            $__kata__ = $this->deleteDerivationSuffixes($__kata);
            if ($this->cekKamus($__kata__)) {
                return $__kata__;
            }
            /*-- Cek luluh -l----------*/
            $__kata = preg_replace('/^(keber)/','r',$kata);
            if ($this->cekKamus($__kata)) {
                return $__kata;
            }
            $__kata__ = $this->deleteDerivationSuffixes($__kata);
            if ($this->cekKamus($__kata__)) {
                return $__kata__;
            }
        }
        if (preg_match('/^(keter)\S{1,}/',$kata)) {
            $__kata = preg_replace('/^(keter)/','',$kata);
            if ($this->cekKamus($__kata)) {
                return $__kata;
            }
            $__kata__ = $this->deleteDerivationSuffixes($__kata);
            if ($this->cekKamus($__kata__)) {
                return $__kata__;
            }
            /*-- Cek luluh -l----------*/
            $__kata = preg_replace('/^(keter)/','r',$kata);
            if ($this->cekKamus($__kata)) {
                return $__kata;
            }
            $__kata__ = $this->deleteDerivationSuffixes($__kata);
            if ($this->cekKamus($__kata__)) {
                return $__kata__;
            }
        }
        if (preg_match('/^(berke)\S{1,}/',$kata)) {
            $__kata = preg_replace('/^(berke)/','',$kata);
            if ($this->cekKamus($__kata)) {
                return $__kata;
            }
            $__kata__ = $this->deleteDerivationSuffixes($__kata);
            if ($this->cekKamus($__kata__)) {
                return $__kata__;
            }
        }
        /* --- Cek Ada Tidaknya Prefik/Awalan ("di-", "ke-", "se-", "te-", "be-", "me-", atau "pe-") ------*/
        if (preg_match('/^(di|[kstbmp]e)\S{1,}/',$kata) == FALSE) {
            return $kataAsal;
        }
        
        return $kataAsal;
    }
}
